package com.imoblife.brainwavestus.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.imoblife.brainwavestus.R;
import com.imoblife.brainwavestus.customview.ListViewItem;
import com.imoblife.brainwavestus.interfaces.IPayType;
import com.imoblife.brainwavestus.interfaces.IUserAction;
import com.imoblife.brainwavestus.model.Constant;
import com.imoblife.brainwavestus.pay.CashPay;
import com.imoblife.brainwavestus.pay.GoldPay;
import com.imoblife.brainwavestus.product.Product;
import com.imoblife.brainwavestus.product.ProductInfo;
import com.imoblife.brainwavestus.product.Store;
import com.imoblife.brainwavestus.user.ActionAudition;
import com.imoblife.brainwavestus.user.ActionBuy;
import com.imoblife.brainwavestus.user.ActionObtain;
import com.imoblife.brainwavestus.user.User;
import com.imoblife.brainwavestus.util.DebugUtil;

public class ProductBuyAdapter extends LvBaseProductAdapter {
	
	private User mUser;
	private Activity mActivity;
	private Store mStore;
	private HashMap<String, ListViewItem> map;

	public ProductBuyAdapter(Context ctx, int layoutId, ArrayList<Product> list) {
		super(ctx, layoutId, list);		
		
		map = new HashMap<String, ListViewItem>();
	}
	
	public void setUser(User user){
		
		mUser = user;
	}
	
	public void setStore(Store store) {
		
		mStore = store;
	}
	
	public void setActivity(Activity activity) {
		
		mActivity = activity;
	}
	
	public void clearAllItemsData() {
		
		if(map != null) {
			
			map.clear();
		}
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		final ProductBuyViewHolder vh;
		final int pos = position;
		
		if (convertView == null) {

			convertView = mInflater.inflate(mLayoutId, null);
			
			vh = new ProductBuyViewHolder();

			vh.tvProductTitle = (TextView) convertView
					.findViewById(R.id.name);
			
			vh.tvProductOldPrice = (TextView) convertView
					.findViewById(R.id.old_price);
			
			vh.tvProductNewPrice = (TextView) convertView
					.findViewById(R.id.price_text);
			
			vh.tvProductGoldPrice = (TextView) convertView
					.findViewById(R.id.tus_price_text);
			
			vh.tvProductSize = (TextView) convertView
					.findViewById(R.id.length_text);
			
			vh.tvProductHz = (TextView) convertView
					.findViewById(R.id.brainstate_text);
			vh.tvProductShortDescrption = (TextView) convertView
					.findViewById(R.id.description_text);
			vh.cbProductAudition = (CheckBox) convertView
					.findViewById(R.id.cb_product_audition);
			
			vh.btBuy = (Button) convertView
					.findViewById(R.id.buy);
			
			vh.btGetFree = (Button) convertView
					.findViewById(R.id.getfree);
			
			vh.pb = (ProgressBar) convertView
					.findViewById(R.id.pb_download_progress);
			
			vh.pbLength = (TextView) convertView
					.findViewById(R.id.tv_download_text);
			
			convertView.setTag(vh);
		} else {
			vh = (ProductBuyViewHolder) convertView.getTag();
		}

		if (mProductData == null) {

			return null;
		}
		final Product product = mProductData.get(position);

		if (product == null) {

			return null;
		}

		final ProductInfo info = product.getProductInfo();
		
		//测试进度条
		((View)vh.pb.getParent()).setVisibility(View.VISIBLE);
		
		vh.tvProductTitle.setText(info.getTitle());
		
		String oldPrice = info.getOriginalPrice();
		String newPrice = info.getPriceOfDollar();
		String priceOfGold = info.getPriceOfGold();
		
		if (!oldPrice.equals(ProductInfo.INIT_PRICE)) {
			
			((View)vh.tvProductOldPrice.getParent())
				.setVisibility(View.VISIBLE);
			vh.tvProductOldPrice.setText(oldPrice);		
		}else {
			
			((View)vh.tvProductOldPrice.getParent())
			.setVisibility(View.GONE);
		
		}
		//priceOfGold = ProductUtil.getTUSGold(newPrice);
		
		vh.tvProductNewPrice.setText(newPrice);
		vh.tvProductGoldPrice.setText(priceOfGold);
		vh.tvProductSize.setText(info.getSize());
		vh.tvProductHz.setText(info.getHzOfProduct());
		vh.tvProductShortDescrption.setText(
				mCtx.getResources().getString(R.string.description)
				+ info.getShortDecription());
		
		String downState = info.getDownloadState();
		
		if(downState == Constant.DOWNLOAD_STATE_START){
			
			((View)vh.pb.getParent()).setVisibility(View.VISIBLE);
		}else {
			
			((View)vh.pb.getParent()).setVisibility(View.GONE);
		}
		
		if(!downState.equals(Constant.DOWNLOAD_STATE_NOT_ENBLE)){
			
			DebugUtil.debug("list view state: ", downState);
			vh.btGetFree.setVisibility(View.GONE);
		}else {
			
			vh.btGetFree.setVisibility(View.VISIBLE);
		}
		
		//合集产品，不显示试听按钮
		if (info.isProductCollection()) {
			
			convertView.setBackgroundColor(
					Constant.LV_BG_COLOR_OF_LAST_PRODUCT);
			
			vh.cbProductAudition.setVisibility(View.INVISIBLE);
		}else {
			
			convertView.setBackgroundColor(
					Color.TRANSPARENT);
			
			vh.cbProductAudition.setVisibility(View.VISIBLE);
			
			vh.cbProductAudition.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					
					if (mUser != null) {
						IUserAction action = new ActionAudition();
						action.setView(v);
						action.setActivity(mActivity);
						mUser.setUserAction(action);
						mUser.act(product);
					}
				}
			});
		}	
		
		if(position == (mProductData.size() - 1)) {
			
			convertView.setBackgroundColor(
					Constant.LV_BG_COLOR_OF_LAST_PRODUCT);
		}

		vh.btBuy.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if (mUser != null) {
					
					IPayType payType = new CashPay();
					IUserAction action = new ActionBuy(payType);
					action.setActivity(mActivity);
					mUser.setUserAction(action);
					mUser.act(product);
				}
			}
		});
		
		vh.btGetFree.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				
				if (mUser != null) {
					
					IPayType payType = new GoldPay(mActivity);
					
					payType.setStore(mStore);
					payType.setUser(mUser);
					IUserAction action = new ActionObtain(payType);
					action.setActivity(mActivity);
					mUser.setUserAction(action);
					boolean isPaySuccess = mUser.act(product);
					
					if(isPaySuccess) {
						
						DebugUtil.debug("productBuyAdapter","pay success, begin update ui");
					}
				}
			}
		});	
		
		if(map != null ) {
			
			String name = info.getId();
			
			if(map.containsKey(name)) {			
				//mPurchaseProduct.removeItem(name);
			}else {
				
				ListViewItem lv = new ListViewItem(name, vh, mActivity);
				lv.setStore(mStore);
				map.put(name, lv);
			}										
		}
		
		return convertView;
	}
	
	public void updateUiList() {
		
		ArrayList<ListViewItem> items = null;
		
		if(map != null) {
			
			items = new ArrayList<ListViewItem>(map.values());
		}
		
		for(ListViewItem item: items){
			
			item.updateUI();
		}
	}
	
	public HashMap<String,ListViewItem> getMap() {
		
		return map;
	}
	
	public class ProductBuyViewHolder {
		//因为初始的xml文件里面，将name属性作为脑波的id区分，所以，随语言显示的脑波的名字，其实是ProductInfo
		//里面的title属性，所以，需要显示脑波名字的地方，需要调用它的title属性	
		public TextView tvProductTitle;
		public TextView tvProductOldPrice;
		public TextView tvProductNewPrice;
		public TextView tvProductGoldPrice;
		public TextView tvProductSize;
		public TextView tvProductHz;
		public TextView tvProductShortDescrption;
		//试听按钮
		public CheckBox cbProductAudition;
		//购买按钮
		public Button btBuy;
		//免费获取按钮
		public Button btGetFree;
		public ProgressBar pb;
		public TextView pbLength;
	}
}
